package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.Util.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.entity.vo.GetDishVo;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.IDishFlavorService;
import com.itheima.reggie.service.IDishService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.naming.spi.ResolveResult;
import java.util.List;

/**
 * <p>
 * 菜品管理 服务实现类
 * </p>
 *
 * @author 陈振豪
 * @since 2024-10-02
 */
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements IDishService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private IDishFlavorService dishFlavorService;

    /**
     * 分页获取菜单
     *
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public R getDishPage(int page, int pageSize,String name) {
        int offset = (page - 1) * pageSize;
        RowBounds rowBounds = new RowBounds(offset, pageSize);
        List<Dish> dishPage = dishMapper.getDishPage(rowBounds, name);
        Page page1 = new Page(dishPage, dishPage.size(), page, pageSize);
        return R.success(page1);
    }

    /**
     * 获取一个菜品
     *
     * @return
     */
    @Override
    public R<GetDishVo> getOneDish(Long id) {
        Dish dish = dishMapper.selectById(id);
//        Dish dish = getById(id);
        GetDishVo dishDto = new GetDishVo();

        /*LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dish.getId());
        List<DishFlavor> flavors = dishFlavorService.list(queryWrapper);*/

        List<DishFlavor> flavors = dishFlavorService.selectByDishId(id);

        BeanUtils.copyProperties(dish,dishDto);
        dishDto.setFlavors(flavors);
        return R.success(dishDto);
    }
}
